﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace XWord.WPF.ApplicationFramework
{
    public class UIElementService
    {
        public static bool GetAttachEventState(DependencyObject obj)
        {
            return (bool)obj.GetValue(AttachEventStateProperty);
        }

        public static void SetAttachEventState(DependencyObject obj, bool value)
        {
            obj.SetValue(AttachEventStateProperty, value);
        }

        public static readonly DependencyProperty AttachEventStateProperty =
            DependencyProperty.RegisterAttached("AttachEventState", typeof(bool), typeof(UIElementService),
            new UIPropertyMetadata(false, (o, a) =>
            {
                new AttachEventBehavior(o as UIElement);
            }));

        internal class AttachEventBehavior
        {
            private UIElement _element;

            public AttachEventBehavior(UIElement element)
            {
                _element = element;
                _element.MouseLeftButtonDown += new MouseButtonEventHandler(_element_MouseLeftButtonDown);
                _element.MouseLeftButtonUp += new MouseButtonEventHandler(_element_MouseLeftButtonUp);
            }

            void _element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                UIElementService.SetIsMouseDown(sender as DependencyObject, false);
                UIElementService.SetIsMouseUp(sender as DependencyObject, true);
            }

            void _element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                UIElementService.SetIsMouseUp(sender as DependencyObject, false);
                UIElementService.SetIsMouseDown(sender as DependencyObject, true);
            }
        }

        public static bool GetIsMouseDown(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsMouseDownProperty);
        }

        public static void SetIsMouseDown(DependencyObject obj, bool value)
        {
            obj.SetValue(IsMouseDownProperty, value);
        }

        public static readonly DependencyProperty IsMouseDownProperty =
            DependencyProperty.RegisterAttached("IsMouseDown", typeof(bool), typeof(UIElementService),
            new UIPropertyMetadata(false));

        public static bool GetIsMouseUp(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsMouseUpProperty);
        }

        public static void SetIsMouseUp(DependencyObject obj, bool value)
        {
            obj.SetValue(IsMouseUpProperty, value);
        }

        public static readonly DependencyProperty IsMouseUpProperty =
            DependencyProperty.RegisterAttached("IsMouseUp", typeof(bool), typeof(UIElementService),
            new UIPropertyMetadata(false));

        public static Key GetKey(DependencyObject obj)
        {
            return (Key)obj.GetValue(KeyProperty);
        }

        public static void SetKey(DependencyObject obj, Key value)
        {
            obj.SetValue(KeyProperty, value);
        }

        public static readonly DependencyProperty KeyProperty =
            DependencyProperty.RegisterAttached("Key", typeof(Key), typeof(UIElementService),
            new UIPropertyMetadata(Key.None));
    }
}
